package com.wanxi.service.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wanxi.dao.ImageDao;
import com.wanxi.dto.ResultDTO;
import com.wanxi.model.Image;
import com.wanxi.service.ImageService;
import com.wanxi.util.CreateOnlyID;
import com.wanxi.util.JsonCopy;
import com.wanxi.vo.ImageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author Chengbo Zhang
 */
@Service
@Transactional
public class ImageServiceImpl implements ImageService {
    @Autowired
    ImageDao imageDao;
    @Autowired
    CreateOnlyID createOnlyId;
    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Override
    public ResultDTO findAllImage() {
        QueryWrapper<Image> queryWrapper=new QueryWrapper<Image>()
                .orderByDesc("id");
        List<Image> list = imageDao.selectList(queryWrapper);
        if (list.size() > 0) {
            return ResultDTO.getDTO(list.size(), list);
        }
        return ResultDTO.builder().data(list).msg("no data").count(0).code(1).build();
    }

    @Override
    public ResultDTO findImageById(ImageVO imageVO) {
        Image image = JsonCopy.copy(imageVO, Image.class);
        Image img = imageDao.selectById(image.getId());
        if (img != null) {
            return ResultDTO.getDTO(1, img);
        }
        return ResultDTO.builder().count(0).msg("error").code(1).build();
    }

    @Override
    public ResultDTO updateEnable(ImageVO imageVO) {
        Image image = JsonCopy.copy(imageVO, Image.class);
        image.setUpdateTime(new Date());
        int i = imageDao.updateById(image);
        if (i == 1) {
            stringRedisTemplate.delete("image");
            return ResultDTO.getDTO(i, imageVO);
        }
        return ResultDTO.builder().data(imageVO).count(0).code(1).msg("error").build();
    }

    @Override
    public ResultDTO addImage(ImageVO imageVO) {
        Image image = JsonCopy.copy(imageVO, Image.class);
        image.setId(createOnlyId.createId());
        image.setCreateTime(new Date());
        image.setUpdateTime(new Date());
        int i = imageDao.insert(image);
        if (i == 1) {
            stringRedisTemplate.delete("image");
            return ResultDTO.getDTO(i, imageVO);
        }
        return ResultDTO.builder().code(1).count(0).msg("error").data(imageVO).build();
    }

    @Override
    public ResultDTO deleteImage(ImageVO imageVO) {
        Image image = JsonCopy.copy(imageVO, Image.class);
        int i = imageDao.deleteById(image);
        if (i == 1) {
            stringRedisTemplate.delete("image");
            return ResultDTO.getDTO(i, imageVO);
        }
        return ResultDTO.builder().code(1).count(0).msg("error").data(imageVO).build();
    }

    @Override
    public ResultDTO update(ImageVO imageVO) {
        Image image = JsonCopy.copy(imageVO, Image.class);
        image.setUpdateTime(new Date());
        int i = imageDao.updateById(image);
        if (i == 1) {
            stringRedisTemplate.delete("image");
            return ResultDTO.getDTO(i, imageVO);
        }
        return ResultDTO.builder().code(1).count(0).msg("error").data(imageVO).build();
    }

    @Override
    public ResultDTO findImage() {
        Long len = stringRedisTemplate.opsForList().size("image");
//        判断redis中是否有数据
        if (len != 0) {
//            返回redis中的数据,通过fastJson转换
            List<String> redisImage = stringRedisTemplate.opsForList().range("image", 0, len);
            List<Image> list = JSON.parseObject(redisImage.toString(), new TypeReference<List<Image>>() {
            });
            return ResultDTO.getDTO(list.size(), list);
        }
//        没有则从数据库中找数据
        QueryWrapper<Image> queryWrapper=new QueryWrapper<Image>()
                .orderByDesc("id");
        List<Image> list = imageDao.selectList(queryWrapper);
        if (list.size() > 0) {
//            Dao层数据迭代
            Iterator<Image> iterator = list.iterator();
            while (iterator.hasNext()) {
                Image i = iterator.next();
                if (i.getEnable() == 0) {
                    iterator.remove();
                } else {
//                    将查到的数据放入redis
                    stringRedisTemplate.expire("image",600, TimeUnit.SECONDS);
                    stringRedisTemplate.opsForList().rightPush("image", JSON.toJSONString(i));
                }
            }
            return ResultDTO.getDTO(list.size(), list);
        }
        return ResultDTO.builder().data(list).msg("no data").count(0).code(1).build();
    }
}
